package cn.hiapi.admin.api;

import cn.hiapi.admin.query.ActivityOrderQuery;
import cn.hiapi.admin.vo.ActivityOrderVo;
import cn.hiapi.admin.vo.OrderExcel;
import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.entity.BasicEntity;
import cn.hiapi.basic.excel.IExcelConverter;
import cn.hiapi.basic.exception.BasicException;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.interfaces.user.IUserService;
import cn.hiapi.shop.entity.shop.Shop;
import cn.hiapi.shop.service.shop.ShopService;
import cn.hiapi.shopkeeper.entity.order.ActivityOrder;
import cn.hiapi.shopkeeper.service.order.ActivityOrderService;
import cn.hiapi.user.dto.BasicUser;
import cn.hiapi.user.service.UserService;
import cn.hiapi.utils.DateUtils;
import cn.hiapi.utils.JsonUtils;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2022/12/18 17:31
 */
@RestController("manageActivityOrderController")
@RequestMapping("/manage/activity-order")
public class ActivityOrderController extends BasicQueryController<ActivityOrder, Long, ActivityOrderVo, ActivityOrderQuery> {
    protected ActivityOrderController(ActivityOrderService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
    }

    @Override
    public ActivityOrderQuery setQuery(ActivityOrderQuery query) {
        if (StringUtils.isNotEmpty(query.getMobile())) {
            Long uid = this.dispatchContext.getServiceOne(IUserService.class).getUid(0, query.getMobile());
            if (uid == null) throw new BasicException("买家不存在");
            query.setBuyerId(uid);
        }
        return super.setQuery(query);
    }

    @Override
    public List<ActivityOrderVo> toListVo(List<ActivityOrder> list, String[] fields) {
        return list.stream().map(e -> this.toData(e, fields)).collect(Collectors.toList());
    }

    @Override
    public ActivityOrderVo toData(ActivityOrder data, String[] fields) {
        ActivityOrderVo vo = data.copy(new ActivityOrderVo());
        Shop shop = this.dispatchContext.getServiceOne(ShopService.class).get(data.getShopId());
        if (shop != null) {
            vo.setShop(JsonUtils.builder().put("name", shop.getName())
                    .put("logo", shop.getLogo())
                    .build());
        }
        BasicUser basic = this.dispatchContext.getServiceOne(UserService.class).getBasic(data.getBuyerId());
        if (basic != null) vo.setBuyer(basic);

        if (data.getGiftDirection() > 0) {
            BasicUser giftUser = this.dispatchContext.getServiceOne(UserService.class).getBasic(data.getGiftUid());
            vo.setGiftUser(giftUser);
        }
        if (StringUtils.isNotEmpty(data.getOtherText())) {
            vo.setOther(JSON.parseObject(data.getOtherText()));
        }
        return vo;
    }


    @Override
    protected IExcelConverter<ActivityOrder, OrderExcel> getExcelConverter() {
        return new IExcelConverter<ActivityOrder, OrderExcel>() {
            @Override
            public OrderExcel conver(ActivityOrder order) {

                OrderExcel excel = new OrderExcel();
                excel.setId(String.valueOf(order.getId()));
                excel.setTitle(order.getTitle());
                if (order.getActivityType() != null)
                    excel.setActivityType(order.getActivityType().getName());
                Shop shop = dispatchContext.getServiceOne(ShopService.class).get(order.getShopId());
                if (shop != null)
                    excel.setShopName(shop.getName());
                if (order.getCostPrice() != null)
                    excel.setCosPrice(order.getCostPrice().setScale(2, RoundingMode.HALF_EVEN).toPlainString());
                if (order.getDiscount() != null)
                    excel.setDiscount(order.getDiscount().setScale(2, RoundingMode.HALF_EVEN).toPlainString());
                if (order.getSugar() != null)
                    excel.setSurge(order.getSugar().setScale(2, RoundingMode.HALF_EVEN).toPlainString());
                if (order.getPayment() != null)
                    excel.setPayment(order.getPayment().setScale(2, RoundingMode.HALF_EVEN).toPlainString());

                switch (order.getPayType()) {
                    case "money":
                        excel.setPayType("余额");
                        break;
                    case "weixin":
                    case "shopkeeper_user_wx_pay:WXPAY_PROGRAM":
                        excel.setPayType("微信");
                        break;
                    default:
                        excel.setPayType(order.getPayType());
                        break;
                }
//                excel.setPayType(order.getPayType());
                BasicUser basic = dispatchContext.getServiceOne(UserService.class).getBasic(order.getBuyerId());
                if (basic != null)
                    excel.setMobile(basic.getMobile());
                excel.setCreated(DateUtils.format(order.getCreated()));
                if (order.getGiftDirection() == 1) {
                    excel.setGiftStatus("转出");
                } else if (order.getGiftDirection() == 2) {
                    excel.setGiftStatus("转入");
                } else {
                    excel.setGiftStatus("");
                }
                return excel;
            }

            @Override
            public Class<OrderExcel> getClasses() {
                return OrderExcel.class;
            }
        };
    }
}
